import waves from "@/directive/waves";
import { alertWithCallback, alertMessage, isValidateNull } from "@/utils/utils";
import { mapGetters } from 'vuex';
import { getSystemStructureTree, deleteSelectedDepartment, saveDepartment } from '../personnel-manage.service';

export default {
  name: 'department-management',
  components: {},
  directives: { waves },
  data() {
    return {
      data: [], // 列表数据
      editDialogVisible: false, // 编辑详情模态窗
      selectedDataDetail: {}, // 选中数据详情
      editForm: {}, // 编辑用户表单
      editRules: {
        text: [{ required: true, trigger: "change", validator: isValidateNull }]
      }, // 编辑表单验证规则
      dialogTitle: '' // 编辑弹窗标题
    }
  },
  computed: {
    ...mapGetters([
      'codesCheck'
    ])
  },
  created() {
    this.getDataList();
  },
  methods: {
    // 获取列表
    getDataList() {
      this.loading = true;
      getSystemStructureTree('').then(res => {
        this.data = res.data;
      });
    },

    // 打开新建、编辑窗体
    openEditDialog(type, selectedData) {
      if (type === 'create') {
        this.dialogTitle = '新建组织';
        // 重置必填项
        setTimeout(() => {
          this.$refs.dataForm.resetFields();
          this.editForm = {
            parentId: selectedData.id,
            parentName: selectedData.text,
            isEnable: 1
          };
        }, 100);
      } else {
        this.dialogTitle = '编辑组织';
        setTimeout(() => {
          selectedData.parentId = selectedData.parentid;
          this.editForm = Object.assign({}, selectedData);
          this.$refs.dataForm.resetFields();
        }, 100);
      }
      this.editDialogVisible = true;
    },

    // 删除选中数据
    deleteSelectedData(id) {
      alertWithCallback('此操作将永久删除该数据, 是否继续?', '提示').then(() => {
        deleteSelectedDepartment([id]).then((res) => {
          alertMessage(res.message);
          this.getDataList();
        }).catch(() => {}); // 关闭confirm框的回调，不处理的话会报一个js error
      }).catch(() => {}); // 关闭confirm框的回调，不处理的话会报一个js error
    },

    // 创建或者编辑信息
    save() {
      // 表单验证
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          saveDepartment(this.editForm).then(() => {
            this.editDialogVisible = false;
            this.getDataList();
          }).catch(() => {}); // 关闭confirm框的回调，不处理的话会报一个js error
        }
      });
    }
  }
}
